package com.example.warehouse.bus.dao.persist.repository.impl;


import com.example.warehouse.bus.dao.persist.mapper.BusCustomerMapper;
import com.example.warehouse.bus.dao.persist.repository.IBusCustomerRepository;
import com.example.warehouse.bus.pojo.entity.BusCustomer;
import com.example.warehouse.bus.pojo.vo.BusCustomerStandardVO;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.util.PageInfoToPageDataConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

/**
 * @author Linger
 * @date 2023-07-03 11:22
 * @desciption: 处理客户数据的实现类
 */
@Slf4j
@Repository
public class BusCustomerRepositoryImpl implements IBusCustomerRepository {
    @Autowired
    private BusCustomerMapper busCustomerMapper;

    public BusCustomerRepositoryImpl() {
        log.info("创建控制器对象:BusCustomerRepositoryImpl");
    }

    /**
     * 增
     * @param busCustomer 客户数据
     * @return 受影响的行数
     */
    @Override
    public int insert(BusCustomer busCustomer) {
        log.debug("开始执行【新增客户】的数据访问，参数：{}", busCustomer);
        return busCustomerMapper.insert(busCustomer);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除客户】的数据访问，参数：{}", id);
        return busCustomerMapper.deleteById(id);
    }

    @Override
    public int update(BusCustomer busCustomer) {
        log.debug("开始执行【修改客户】的数据访问，参数：{}", busCustomer);
        return busCustomerMapper.updateById(busCustomer);
    }

    @Override
    public BusCustomerStandardVO getStandardById(Long id) {
        log.debug("通过id查询所有公司信息,参数：{}",id);
        return busCustomerMapper.getStandardById(id);
    }

    @Override
    public PageData<BusCustomerStandardVO> queryByKeyWord(Integer pageNum, Integer pageSize,String customerName, String connectionPerson, String phone) {
        log.debug("模糊查询,{},{},{}",customerName, connectionPerson, phone);
        PageHelper.startPage(pageNum, pageSize);
        List<BusCustomerStandardVO> list = busCustomerMapper.queryByKeyWord(customerName, connectionPerson, phone);
        PageInfo<BusCustomerStandardVO> pageInfo = new PageInfo<>(list);
        PageData<BusCustomerStandardVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<BusCustomer> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询客户列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<BusCustomer> list = busCustomerMapper.list();
        PageInfo<BusCustomer> pageInfo = new PageInfo<>(list);
        PageData<BusCustomer> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<BusCustomer> listAsc(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【升序查询客户列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<BusCustomer> list = busCustomerMapper.listAsc();
        PageInfo<BusCustomer> pageInfo = new PageInfo<>(list);
        PageData<BusCustomer> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<BusCustomer> listDesc(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【降序查询客户列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<BusCustomer> list = busCustomerMapper.listDesc();
        PageInfo<BusCustomer> pageInfo = new PageInfo<>(list);
        PageData<BusCustomer> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public int updateById(BusCustomer user) {
        log.debug("开始执行【更新客户】的数据访问，参数：{}", user);
        return busCustomerMapper.updateById(user);
    }


    @Override
    public int countByCustomerName(String customerName) {
        log.debug("开始执行【统计客户数量】的数据访问，参数：{}", customerName);
        return busCustomerMapper.countByCustomerName(customerName);
    }

    @Override
    public int countByPhone(String phone) {
        log.debug("开始执行【统计手机号码数量】的数据访问，参数：{}", phone);
        return busCustomerMapper.countByPhone(phone);
    }

    @Override
    public int countByEmail(String email) {
        log.debug("开始执行【统计电子邮箱数量】的数据访问，参数：{}", email);
        return busCustomerMapper.countByEmail(email);
    }



}
